import { _decorator, Component, Label, Slider, ProgressBar } from 'cc';
import { VMCenter } from '../../framework/vm/VMCenter';
const {ccclass,inspector, property} = _decorator;

import { mvvm, VMChildren, VMLabel, VMProgress } from "../../framework/vm/VMDecorator";
import { VMEntity } from '../../framework/vm/VMEntity';
import { userData } from '../../game/storage/UserData';
import * as cc from "cc";
import { DemoVMSceneBase } from './DemoVMSceneBase';
@ccclass('DemoVMProgress')
// @mvvm()
export default class DemoVMProgress extends DemoVMSceneBase implements IMVVM{
    _vmTag: string;
    _vmEntitys: VMEntity[];
  
    // @VMLabel({watchPath: ['*.hp','userdata.mhp']})
    @property(Label)
    HPLabel: Label | null = null;
 
    // @VMProgress({watchPath: ['*.hp','userdata.mhp']})
    @property(Slider)
    HPController: Slider | null = null;
   
    // @VMProgress({watchPath: ['*.hp','userdata.mhp']})
    @property(ProgressBar)
    HPProgress: ProgressBar | null = null;

    data = {
        hp: 90
    }
    
  
    start() {
        super.start();
    }


    onEnable(){
        // 手动添加 滑块的滑动事件
        let handler = new cc.EventHandler();
        handler.target = this.node;
        handler.component = "DemoVMProgress";
        handler.handler = "onHPControllerEvent";
        this.HPController.slideEvents.push(handler);


        VMCenter.add(this.data,this);
        VMCenter.add(userData,"userdata");
        VMCenter.Label(this,this.HPLabel,{watchPath: ['*.hp','userdata.mhp']})
        VMCenter.Progress(this,this.HPProgress,{watchPath: ['*.hp','userdata.mhp']})
        VMCenter.Slider(this,this.HPController,{watchPath: ['*.hp','userdata.mhp']})
    }

    onDisable(){

        VMCenter.remove(this);
    }

    
    onEditBoxEnded(event) {
        console.log('DemoVMProgress-> ', event.string);

        let num = parseInt(event.string);
        if (isNaN(num)) {
            return;
        }
        this.data.hp = Math.min(num,userData.mhp);

    }

    onHPControllerEvent(event){
        let hp = Math.round(userData.mhp * event.progress);
        this.data.hp = hp;
        console.log('DemoVMProgress-> ',hp);
    }
}

